package com.jilujia.domain;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.jilujia.domain.hotel.Hotel;
import com.jilujia.domain.hotel.service.HotelSearchCriteria;

/**
 * 该Specifications只应放置可变条件的搜索
 * 
 * @author john.zhou
 * @version $Id: JilujiaSpecifications.java 113 2012-06-28 03:48:18Z catton2000@gmail.com $
 * @notes Created on Jun 28, 2012<br>
 *        Revision of last commit:$Revision: 113 $<br>
 *        Author of last commit:$Author: catton2000@gmail.com $<br>
 *        Date of last commit:$Date: 2012-06-28 03:48:18 +0000 (Thu, 28 Jun 2012) $<br>
 *        <p>
 */
public class JilujiaSpecifications {

	public static Specification<Hotel> generateHotelSpecification(final HotelSearchCriteria criteria) {
		Specification<Hotel> specification = new Specification<Hotel>() {
			public Predicate toPredicate(Root<Hotel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				Predicate predicate = cb.conjunction();
				if (StringUtils.isNotEmpty(StringUtils.trim(criteria.getCountry()))) {
					Predicate predicate1 = cb.equal(root.get("country"), criteria.getCountry());
					predicate = cb.and(predicate, predicate1);
				}

				if (StringUtils.isNotEmpty(StringUtils.trim(criteria.getZip()))) {
					Predicate predicate2 = cb.equal(root.get("zip"), criteria.getZip());
					predicate = cb.and(predicate, predicate2);
				}

				return predicate;
			}
		};
		return specification;
	}

	// public static Specification<Answer> generateAnswerSpecification(final Long questionId) {
	// Specification<Answer> specification = new Specification<Answer>() {
	// public Predicate toPredicate(Root<Answer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	//
	// Predicate predicate = cb.conjunction();
	//
	// Join<Answer, Question> questionJoin = root.join("question", JoinType.INNER);
	// Predicate predicate2 = cb.equal(questionJoin.get("id"), questionId);
	//
	// predicate = cb.and(predicate, predicate2);
	//
	// return predicate;
	// }
	// };
	// return specification;
	// }
	//
	// public static Specification<AnswerComment> generateAnswerCommentSpecification(final Long answerId) {
	// Specification<AnswerComment> specification = new Specification<AnswerComment>() {
	// public Predicate toPredicate(Root<AnswerComment> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	//
	// Predicate predicate = cb.conjunction();
	//
	// Join<AnswerComment, Answer> answerJoin = root.join("answer", JoinType.INNER);
	// Predicate predicate2 = cb.equal(answerJoin.get("id"), answerId);
	//
	// predicate = cb.and(predicate, predicate2);
	//
	// return predicate;
	// }
	// };
	// return specification;
	// }
}
